var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const pool = require('./pool')
const cors = require('cors')
var app = express();
app.use(cors({
	origin:'*'
}))
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

// 处理post参数的JSON数据
app.use(express.json())
// 首页模块
const index = require('./routes/index')
app.use('/v4/index',index)
// 引入用户模块
const user = require('./routes/user')
app.use('/api/users', user)
// 引入后台管理模块
const admin = require('./routes/admin')
app.use('/api/admin', admin)
// 引入商品模块
const product=require('./routes/product')
app.use('/api/product',product)
// 引入拍卖会模块
const auction=require('./routes/auction')
app.use('/api/auction',auction)
// 引入新闻模块
const news=require('./routes/news')
app.use('/api/news',news)
// 搜索接口
app.get('/search',(req,res,next)=> {
	pool.query('SELECT title FROM su_product where title like ?',[`%${req.query.title}%`],(err,result)=> {
		if(err){
			next(err)
			return
		}
		res.send({statusCode:200,msg:'查询成功',result})
	})
})

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
